﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GAMS.MODEL;
using System.Configuration;

namespace KPIS.GERP.GAMS.DAL
{
    public class D_ACM_Account_Version
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public IList<M_ACM_Account_Version> GetList()
        {
            return GetList(false, null);
        }

        public IList<M_ACM_Account_Version> GetList(bool isUsed, int? version_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_ACM_Account_Version> infoList = new List<M_ACM_Account_Version>();
            try
            {
                dbManager.Open();

                strSQL = "SELECT version_seq,version_no,version_descr,record_status"
                    + ",created_by,created_when,updated_by,use_flag ,updated_when"
                    + ",case use_flag when 1 then 'ยังใช้งานอยู่'"
                    + " else 'ไม่ได้ใช้งาน'"
                    + " end as use_flag_name"
                    + " FROM acm_mas_account_version"
                    + " WHERE record_status not like 'D'"
                    + (isUsed ? " AND use_flag = 1" : "")
                    + (version_seq != null ? " AND version_seq != " + version_seq : "")
                    + " ORDER BY version_no";

                //strSQL = "SELECT version_seq,version_no,version_descr,record_status "
                //         + " ,created_by,created_when,updated_by,use_flag "
                //         + " ,updated_when FROM acm_mas_account_version "
                //         + " WHERE record_status = 1"
                //         + " ORDER BY version_no";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ACM_Account_Version info = new M_ACM_Account_Version();
                        info.version_seq = Convert.ToInt32(drc[i]["version_seq"]);
                        info.version_code = drc[i]["version_no"].ToString();
                        info.record_status = drc[i]["record_status"].ToString();
                        info.version_descr = drc[i]["version_descr"].ToString();
                        info.created_by = cn.ConvertNullInt(drc[i]["created_by"]);
                        info.created_when = cn.ConvertNullDateTime(drc[i]["created_when"]);
                        info.use_flag = Convert.ToBoolean(drc[i]["use_flag"]);
                        info.status_name = drc[i]["use_flag_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_ACM_Account_Version GetListFillBySeq(Nullable<int> version_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_ACM_Account_Version info = new M_ACM_Account_Version();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@version_seq", version_seq);

                strSQL = "SELECT version_seq,version_no,version_descr,record_status " 
                         +" ,created_by,created_when,updated_by,use_flag " 
                         +" ,updated_when FROM acm_mas_account_version " 
                         +" WHERE version_seq=@version_seq "
                         +" ORDER BY version_no";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.version_seq = Convert.ToInt32(drc[0]["version_seq"]);
                    info.version_code = drc[0]["version_no"].ToString();
                    info.record_status = drc[0]["record_status"].ToString();
                    info.version_descr = drc[0]["version_descr"].ToString();
                    info.created_by = Convert.ToInt32(drc[0]["created_by"]);
                    info.created_when = Convert.ToDateTime(drc[0]["created_when"]);
                    info.use_flag = Convert.ToBoolean(drc[0]["use_flag"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

       
        public M_ACM_Account_Version GetMaxSeq()
        {
            string strSQL;
            DataRowCollection drc = null;
            M_ACM_Account_Version info = new M_ACM_Account_Version();
            try
            {
                dbManager.Open();
                strSQL = "SELECT MAX(version_seq) as Max_Seq "
                        + " FROM acm_mas_account_version ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.version_seq = Convert.ToInt32(drc[0]["Max_Seq"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
            return info;
        }


        public void Insert(M_ACM_Account_Version info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);
                dbManager.AddParameters(0, "@version_no", info.version_code);
                dbManager.AddParameters(1, "@version_descr", info.version_descr);
                dbManager.AddParameters(2, "@record_status", info.record_status);
                dbManager.AddParameters(3, "@created_by", info.created_by);
                dbManager.AddParameters(4, "@created_when", info.created_when);
                dbManager.AddParameters(5, "@use_flag", info.use_flag);
    
                strSQL = "INSERT INTO acm_mas_account_version "
                    + " (version_no,version_descr "
                    + ", record_status,created_by, created_when,use_flag) "
                    + "  VALUES("
                    + "  @version_no"
                    + ", @version_descr"
                    + ", @record_status"
                    + ", @created_by"
                    + ", @created_when"
                    + ", @use_flag"
                    + " )";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(M_ACM_Account_Version info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(7);
                dbManager.AddParameters(0, "@version_seq", info.version_seq);
                dbManager.AddParameters(1, "@version_no", info.version_code);
                dbManager.AddParameters(2, "@version_descr", info.version_descr);
                dbManager.AddParameters(3, "@record_status", info.record_status);
                dbManager.AddParameters(4, "@updated_by", info.updated_by);
                dbManager.AddParameters(5, "@updated_when", info.updated_when);
                dbManager.AddParameters(6, "@use_flag", info.use_flag);

                strSQL = "UPDATE acm_mas_account_version"
                    + "  SET version_no = @version_no"
                    + ", version_descr = @version_descr"
                    + ", record_status = @record_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + ", use_flag = @use_flag "
                    + " WHERE version_seq = @version_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(M_ACM_Account_Version info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);
                dbManager.AddParameters(0, "@version_seq", info.version_seq);
                dbManager.AddParameters(1, "@updated_by", info.updated_by);
                dbManager.AddParameters(2, "@updated_when", info.updated_when);
                

                strSQL = "UPDATE acm_mas_account_version"
                    + "   SET updated_by  = @updated_by " 
                    + "   ,   updated_when = @updated_when "
                    + "   ,   record_status = 'D' "
                    + " WHERE version_seq = @version_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
